/**
	This file is part of Fighting Field.

	Fighting Field is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Fighting Field is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Fighting Field.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "CheckBox.h"

#include "OgreBorderPanelOverlayElement.h"
#include "OgreTextAreaOverlayElement.h"

#include "GuiListener.h"

namespace FF
{
	CheckBox::CheckBox(const Ogre::String& name, const Ogre::DisplayString& caption, Ogre::Real width)
	{
		mCursorOver = false;
		mFitToContents = width <= 0;
		mElement = Ogre::OverlayManager::getSingleton().createOverlayElementFromTemplate
			("SdkTrays/CheckBox", "BorderPanel", name);
		Ogre::OverlayContainer* c = (Ogre::OverlayContainer*)mElement;
		mTextArea = (Ogre::TextAreaOverlayElement*)c->getChild(GetName() + "/CheckBoxCaption");
		mSquare = (Ogre::BorderPanelOverlayElement*)c->getChild(GetName() + "/CheckBoxSquare");
		mX = mSquare->getChild(mSquare->getName() + "/CheckBoxX");
		mX->hide();
		mElement->setWidth(width);
		SetCaption(caption);
	}

	const Ogre::DisplayString& CheckBox::GetCaption()
	{
		return mTextArea->getCaption();
	}

	void CheckBox::SetCaption(const Ogre::DisplayString& caption)
	{
		mTextArea->setCaption(caption);
		if (mFitToContents) mElement->setWidth(GetCaptionWidth(caption, mTextArea) + mSquare->getWidth() + 23);
	}

	bool CheckBox::IsChecked()
	{
		return mX->isVisible();
	}

	void CheckBox::SetChecked(bool checked, bool notifyListener)
	{
		if (checked) mX->show();
		else mX->hide();
		if (mListener && notifyListener) mListener->OnCheckBoxToggled(this);
	}

	void CheckBox::Toggle(bool notifyListener)
	{
		SetChecked(!IsChecked(), notifyListener);
	}

	void CheckBox::_OnCursorPressed(const Ogre::Vector2& cursorPos)
	{
		if (mCursorOver && mListener) Toggle();
	}

	void CheckBox::_OnCursorMoved(const Ogre::Vector2& cursorPos)
	{
		if (IsCursorOver(mSquare, cursorPos, 5))
		{
			if (!mCursorOver)
			{
				mCursorOver = true;
				mSquare->setMaterialName(mStyle.overMaterialName);
				mSquare->setBorderMaterialName(mStyle.overBorderMaterialName);
			}
		}
		else
		{
			if (mCursorOver)
			{
				mCursorOver = false;
				mSquare->setMaterialName(mStyle.upMaterialName);
				mSquare->setBorderMaterialName(mStyle.upBorderMaterialName);
			}
		}
	}

	void CheckBox::_OnFocusLost()
	{
		mSquare->setMaterialName(mStyle.upMaterialName);
		mSquare->setBorderMaterialName(mStyle.upBorderMaterialName);
		mCursorOver = false;
	}
}
